package com.putong35.common.customviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;

import androidx.annotation.LayoutRes;

import com.putong35.common.R;

/**
 * 一个常用的输入数字的控件，中间一个输入框，左右两边各一个按钮用来自加和自减
 *
 * @Author: PengWeiwei
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2017-05-05
 * @Version: 1.0.0
 */
public class NumberControlView extends FrameLayout {

    private
    @LayoutRes
    int mLayoutId = R.layout.customview_number_control_view;

    /**
     * 最低值
     */
    private int mMinimum = 1;

    /**
     * 最高值
     */
    private int mMaximum = 10;


    private View mBtnMinus;
    private View mBtnPlus;
    private EditText mEtNumber;


    public NumberControlView(Context paramContext) {
        super(paramContext);
    }


    public NumberControlView(Context paramContext, AttributeSet attrs) {
        super(paramContext, attrs);
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.NumberControlView);
            @LayoutRes int layoutId = a.getResourceId(R.styleable.NumberControlView_android_layout, -1);
            if (layoutId > 0) {
                mLayoutId = layoutId;
            }

            mMaximum = a.getInt(R.styleable.NumberControlView_customview_maximum, 10);
            mMinimum = a.getInt(R.styleable.NumberControlView_customview_minimum, 1);

            a.recycle();
        }

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View container = addContainLayout();
        mEtNumber = (EditText) container.findViewById(R.id.customViewsEtNumber);
        mBtnPlus = container.findViewById(R.id.customViewsBtnPlus);
        mBtnMinus = container.findViewById(R.id.customViewsBtnMinus);
        mBtnPlus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int currNum = getNumber();
                if (currNum < mMaximum) {
                    setNumber(currNum + 1);
                    if (numberChangedListener != null) {
                        numberChangedListener.onNumberChanged(currNum, getNumber());
                    }
                }
            }
        });
        mBtnMinus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int currNum = getNumber();
                if (currNum > mMinimum) {
                    setNumber(currNum - 1);
                    if (numberChangedListener != null) {
                        numberChangedListener.onNumberChanged(currNum, getNumber());
                    }
                }
            }
        });
    }

    private View addContainLayout() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(mLayoutId, this, true);
    }

    public int getNumber() {
        String numStr = mEtNumber.getText().toString();
        return Integer.parseInt(numStr);
    }

    public void setNumber(int number) {
        mEtNumber.setText(""+number);
    }

    public void setOnNumberChangedListener(OnNumberChangedListener numberChangedListener) {
        this.numberChangedListener = numberChangedListener;
    }

    private OnNumberChangedListener numberChangedListener;
    public interface OnNumberChangedListener {
        void onNumberChanged(int src, int current);
    }
}
